package com.wx.android.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wx.android.common.R;

/**
 * Created by Administrator on 2016/9/20.
 */

public class ItemView extends RelativeLayout {

    private View itemView;
    private TextView tv_left;
    private ImageView iv_icon;
    private TextView tv_right;
    private ImageView iv_arrow;
    public int BUTTON_SIZE;

    public ItemView(Context context) {
        super(context);
        init(context, null);
    }

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {


        View itemView = LayoutInflater.from(context).inflate(R.layout.item_view, null);
        tv_left = (TextView) itemView.findViewById(R.id.tv_left);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        tv_right = (TextView) itemView.findViewById(R.id.tv_rigth);
        iv_arrow = (ImageView) itemView.findViewById(R.id.iv_arrow);


        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemView);
        String left = a.getString(R.styleable.ItemView_tv_left);
        int icon = a.getResourceId(R.styleable.ItemView_iv_icon, R.mipmap.arrow);
        String right = a.getString(R.styleable.ItemView_tv_right);
        Boolean arrow = a.getBoolean(R.styleable.ItemView_iv_arrow, true);
        a.recycle();


        setIcon(icon);
        setArrow(arrow);
        if (left != null) {
            setLeftView(left);
        }
        if (right != null) {
            setRightView(right);
        }
        this.itemView = itemView;
        BUTTON_SIZE = context.getResources().getDimensionPixelSize(R.dimen.item_hight);
        addView(itemView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, BUTTON_SIZE));
    }

    private void setLeftView(String str) {
        tv_left.setText(str);
    }

    private void setIcon(int id) {
        iv_icon.setImageResource(id);
    }

    private void setRightView(String str) {
        tv_right.setText(str);
    }

    private void setArrow(Boolean boArrow) {
        if (boArrow) {
            iv_arrow.setVisibility(VISIBLE);
        } else {
            iv_arrow.setVisibility(GONE);
        }
    }

    public void setItemOnClick(OnClickListener mOnClickListener) {
        if (mOnClickListener != null) {
            itemView.setOnClickListener(mOnClickListener);
        }
    }

}
